﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Data;
using Twi.DataAccess.DBUtility;

namespace Twi.COMMON.WCF
{
    public static class EMail
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serverHost">服务器地址，入smtp.163.com</param>
        /// <param name="port">端口，一般为25</param>
        /// <param name="fromMailAddress">发信箱地址</param>
        /// <param name="fromMailPwd">发信箱密码</param>
        /// <param name="toMailAddressList">收信箱地址</param>
        /// <param name="mailTitle">邮件主题</param>
        /// <param name="mailContent">邮件正文</param>
        /// <param name="pathList">附件</param>
        public static void SendToEmail(string serverHost, int port, string fromMailAddress, string fromMailDisplayName, string fromMailPwd, string toMailAddress, string mailTitle, string mailContent, List<string> pathList)
        {
            if (string.IsNullOrEmpty(toMailAddress))
            {
                return;
            }
            //检测附件是否存在以及附件的大小
            FileStream FileStream_my = null;
            if (pathList != null && pathList.Count > 0)
            {
                for (int i = 0; i < pathList.Count; i++)
                {
                    if (!File.Exists(pathList[i])) //判断文件是否存在
                        return;
                    else
                    {
                        FileStream_my = new FileStream(pathList[i], FileMode.Open);//附件文件流
                        string name = FileStream_my.Name;
                        long fileSize = FileStream_my.Length;
                        int size = (int)(fileSize / 1024 / 1024);
                        FileStream_my.Close();
                        FileStream_my.Dispose();
                        //控制文件大小不大于10Ｍ                
                        if (size > 10)
                        {
                            throw new Exception("文件长度不能大于10M！");
                        }
                    }
                }
            }
            MailAddress MailAddress_from = null; //设置发信人地址  
            //邮件发送验证
            SmtpClient smtpCilent = new SmtpClient();//设置SMTP协议
            try
            {
                //指定SMTP服务名  
                //例如QQ邮箱为smtp.qq.com新浪cn邮箱为 smtp.sina.cn等
                smtpCilent.Host = serverHost;
                if (port != 000)
                {
                    smtpCilent.Port = port;//指定端口号
                }
                //smtpCilent.Timeout = 0; //超时时间
            }
            catch (Exception ex)
            {
                throw new Exception("邮件发送失败,请确定SMTP服务名是否正确！");
            }
            try
            {
                //验证发件人
                //创建发件人的服务器认证
                NetworkCredential networkCredential_My = new NetworkCredential(fromMailAddress, fromMailPwd);
                //实例化发件人地址     
                MailAddress_from = new System.Net.Mail.MailAddress(fromMailAddress, fromMailDisplayName);
                //指定发件人信息  包括邮箱地址和邮箱密码            
                smtpCilent.Credentials = new System.Net.NetworkCredential(MailAddress_from.Address, fromMailPwd);
            }
            catch (Exception ex)
            {
                throw new Exception("邮件发送失败,请确定发件邮箱地址和密码的正确性！");
            }
            MailMessage MailMessage_Mai = new MailMessage();
            //清空历史发送信息，以防发送时收件人收到的错误信息(收件人列表会不断重复)
            MailMessage_Mai.To.Clear();
            try
            {
                MailMessage_Mai.To.Add(new MailAddress(toMailAddress));
            }
            catch (Exception)
            {
                throw;
            }
            MailMessage_Mai.From = MailAddress_from;//发件人邮箱 
            MailMessage_Mai.Subject = mailTitle;//邮件标题
            MailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8;//文本格式
            MailMessage_Mai.Body = mailContent;//邮件正文  
            MailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8;//文本格式
            MailMessage_Mai.IsBodyHtml = true;
            //清空历史附件  以防附件重复发送               
            MailMessage_Mai.Attachments.Clear();
            if (pathList != null && pathList.Count > 0)
            {
                for (int i = 0; i < pathList.Count; i++)
                {
                    //构造一个附件对象
                    Attachment attach = new Attachment(pathList[i]);
                    //得到文件的信息
                    System.Net.Mime.ContentDisposition disposition = attach.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(pathList[i]);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathList[i]);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(pathList[i]);
                    //向邮件添加附件
                    MailMessage_Mai.Attachments.Add(attach);
                }
            }
            smtpCilent.SendAsync(MailMessage_Mai, "000000000");
        }
    }
}
